import wx
import os

from utils import tracer
from gui.mdiFrame import MDIFrame
from uiManager import UIManager

class MenuManager(UIManager):
  def __init__(self, name):
    UIManager.__init__(self, name)

  def load(self, pluginMgr):
    if pluginMgr is None:
      return

    self._items = {}

    tracer.writeln('Loading menus from plugins ...')
    
    plugins = pluginMgr.getPlugins()
    for plugin in plugins:
      mod = plugin.getModule()
      if mod is None:
        continue
      
      menus = mod.getMenu()
      if menus is None:
        continue

      for menu in menus:
        if menu is None or len(menu) < 2 or menu[0] == '' or menu[1] is None:
          continue
        self._items[menu[0]] = menu
    
    tracer.writeln('Menus loaded. Count: %d%s' % (len(self._items), os.linesep))

  def attach(self, uiParent):
    if uiParent is None:
      return
    
    assert type(uiParent) is MDIFrame
    self._uiParent = uiParent

    tracer.writeln('%s attaching ...' % self.getName())

    menubar = wx.MenuBar()

    tracer.writeln('Creating default menus ...')

    # create File menu
    self.createFileMenu(menubar, self._uiParent)
    # create Edit menu
    self.createEditMenu(menubar, self._uiParent)
    # create View menu
    self.createViewMenu(menubar, self._uiParent)
    # create Help menu
    self.createHelpMenu(menubar, self._uiParent)

    tracer.writeln('Attaching sub menus of other UI managers ...')

    for name, uiMgr in self._uiParent.getUIManagers():
      if name != self.getName():
        menus = uiMgr.getMenus(self._uiParent)
        if menus is None:
          continue

        for menu in menus:
          if menu is None or len(menu) < 2 or menu[0] == '' or menu[1] is None:
            continue
          index = menubar.FindMenu(menu[0])
          if index == wx.NOT_FOUND:
            menubar.Insert(menubar.GetMenuCount() - 1, menu[1], menu[0])
          else:
            existMenu = menubar.GetMenu(index)
            assert not existMenu is None
  
            menuItems = menu[1].GetMenuItems()
            if menuItems is None:
              continue
            insertPos = 0
            for menuItem in menuItems:
              existMenu.InsertItem(insertPos, menuItem)
              insertPos += 1
    
    tracer.writeln('Attaching sub menus of plugins ...')
    
    if not self._items is None and len(self._items.values()) > 0:
      for menu in self.getItems():
        if menu is None or len(menu) < 2 or menu[0] == '' or menu[1] is None:
          continue
        index = menubar.FindMenu(menu[0])
        if index == wx.NOT_FOUND:
          menubar.Insert(menubar.GetMenuCount() - 1, menu[1], menu[0])
        else:
          existMenu = menubar.GetMenu(index)
          assert not existMenu is None

          menuItems = menu[1].GetMenuItems()
          if menuItems is None:
            continue
          insertPos = 0
          for menuItem in menuItems:
            existMenu.InsertItem(insertPos, menuItem)
            insertPos += 1

    self._uiParent.SetMenuBar(menubar)

    tracer.writeln(' Done%s' % os.linesep)

  def createFileMenu(self, menuBar, uiParent):
    file_menu = wx.Menu()
    
    file_menu_item_exit = file_menu.Append(-1, 'E&xit')
    uiParent.Bind(wx.EVT_MENU, uiParent.onExit, file_menu_item_exit)
    
    menuBar.Append(file_menu, '&File')
    
  def createEditMenu(self, menuBar, uiParent):
    edit_menu = wx.Menu()
    
    edit_menu_item_perferences = edit_menu.Append(-1, 'Perferences')
    uiParent.Bind(wx.EVT_MENU, uiParent.onPerferences, edit_menu_item_perferences)
    
    menuBar.Append(edit_menu, '&Edit')

  def createViewMenu(self, menuBar, uiParent):
    view_menu = wx.Menu()
    
    menuBar.Append(view_menu, '&View')
    
  def createHelpMenu(self, menuBar, uiParent):
    help_menu = wx.Menu()
    
    help_menu_item_about = help_menu.Append(-1, 'About')
    uiParent.Bind(wx.EVT_MENU, uiParent.onAbout, help_menu_item_about)
    
    menuBar.Append(help_menu, '&Help')
